package org.xiaojl.modules.drp.controller;

import java.util.List;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.xiaojl.common.base.BaseController;
import org.xiaojl.dwz.DwzAjax;
import org.xiaojl.modules.drp.entity.DrpProductType;
import org.xiaojl.modules.drp.service.DrpProductTypeService;
import org.xiaojl.utils.ToolUtil;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * <p>
 * 产品类型 前端控制器
 * </p>
 *
 * @author xiaojl
 * @since 2017-09-06
 */
@Controller
@RequestMapping("/drp/drpProductType")
public class DrpProductTypeController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(DrpProductTypeController.class);
	private static final String basePagePath = "drp/productType/";
	
	@Autowired
	private DrpProductTypeService thisService;
	
	/**
	 * 查询数据，进入列表页面
	 */
	@RequiresPermissions("drp/drpProductType/list")
	@RequestMapping("/list")
	public ModelAndView list(Model model){
		try {
			String treeData = thisService.selectAllTree("0");
			log.debug("获取到资源页面的treelist："+treeData);
			model.addAttribute("tree", treeData);
			
			return operatePage(basePagePath+"list",model);
		} catch (Exception e) {
			log.error("查询列表数据出现异常:", e);
			return operateError("获取数据失败");
		}
	}
	
	@RequestMapping("/selectList")
	@ResponseBody
	public Object selectList(){
		List<DrpProductType> list = thisService.selectList(new EntityWrapper<DrpProductType>().orderBy("createTime",true));
		return list;
	}
	
	/**
	 * 转到添加页面 
	 */
	@RequiresPermissions("drp/drpProductType/add")
	@RequestMapping("/addUI")
	public ModelAndView addUI(Model model,String pid) {
		try {
			
			model.addAttribute("pid", pid);
			return operatePage(basePagePath+"add",model);
			
		} catch (Exception e) {
			log.error("进入添加页面出现异常:", e);
			return operateError("获取数据失败");
		}
	}
	
	/**
	 * 保存 .
	 */
	@RequiresPermissions("drp/drpProductType/add")
	@RequestMapping("/add")
	public ModelAndView add(Model model, @Valid DrpProductType entity, BindingResult result, DwzAjax dwz) {
		try {
			if(result.hasErrors()){
				return operateError(result.getAllErrors().get(0).getDefaultMessage());
			}
			
			/** 数据唯一性校验 */
			List<DrpProductType> listByName = thisService.selectList(new EntityWrapper<DrpProductType>().eq("name",entity.getName()));
			if(ToolUtil.isNotEmpty(listByName)){
				return operateError("商品类型名称【"+entity.getName()+"】已经存在！");
			}
			
			if(ToolUtil.isNotEmpty(entity.getPid())){
				DrpProductType p = thisService.selectById(entity.getPid());
				if(ToolUtil.isEmpty(p)){
					return operateError("该商品类型的上级商品类型不存在！");
				}
			}else{
				entity.setPid(0L);
			}
			//插入数据库
			thisService.insertWithCacade(entity);
			
			return operateSuccess(dwz);
		} catch (Exception e) {
			log.error("添加数据出现异常:", e);
			return operateError("保存数据失败");
		}
	}
	
	/**
	 * 转到修改页面 .
	 */
	@RequiresPermissions("drp/drpProductType/edit")
	@RequestMapping("/editUI")
	public ModelAndView editUI(Model model, Long id) {
		try {
			DrpProductType entity = thisService.selectById(id);
			if (ToolUtil.isEmpty(entity)) {
				return operateError("获取数据失败");
			}

			model.addAttribute("entity",entity);
			return operatePage(basePagePath+"edit",model);
		} catch (Exception e) {
			log.error("进入编辑页面时，获取数据出现异常:id="+id, e);
			return operateError("获取数据失败");
		}
	}
	
	/**
	 * 保存修改后的信息 .
	 * 
	 */
	@RequiresPermissions("drp/drpProductType/edit")
	@RequestMapping("/edit")
	public ModelAndView edit(@Valid DrpProductType entity, BindingResult result, DwzAjax dwz) {
		try {
			if(result.hasErrors()){
				return operateError(result.getAllErrors().get(0).getDefaultMessage());
			}
			
			Long id = entity.getId();
			DrpProductType oldObj = thisService.selectById(id);
			if (ToolUtil.isEmpty(oldObj)) {
				return operateError("无法获取要修改的数据");
			}
			
			/** 数据唯一性校验逻辑*/
			
			
			//更新数据库
			thisService.updateById(entity);
			
			return operateSuccess(dwz);
		} catch (Exception e) {
			log.error("保存修改数据出现异常:", e);
			return operateError("保存失败");
		}
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("drp/drpProductType/delete")
	@RequestMapping("/delete")
	public ModelAndView delete(Long id, DwzAjax dwz) {
		try {
			DrpProductType entity = thisService.selectById(id);
			if (ToolUtil.isEmpty(entity)) {
				return operateError("无法获取要删除的数据");
			}
			
			thisService.deleteById(id);
			
			return operateSuccess(dwz);
		} catch (Exception e) {
			log.error("删除出现异常:id="+id, e);
			return operateError("删除失败");
		}
	}
	
}
